package com.itheima.controller;

import com.itheima.pojo.Member;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private ProjectInfoAutoConfiguration projectInfoAutoConfiguration;

    // 获取工坊成员集合
    @GetMapping("/getList")
    public Result getMembers() {
        log.info("获取工坊成员集合：");
        List<Member> list = memberService.getList();
        return Result.success(list);
    }

    // 根据id查询工坊成员信息
    @GetMapping("getById")
    public Result getMemberById(@RequestParam int id) {
        log.info("根据id查询工坊成员信息：{}",id);
        Member member = memberService.getById(id);
        return Result.success(member);
    }

    // 分页查询工坊员工
    @GetMapping("/getPage")
    public Result getMembersPage(@RequestParam(defaultValue = "1") Integer page,
                                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询参数：分页查询页码{}，每页记录数{}", page, pageSize);
        PageBean pageBean = memberService.getPage(page,pageSize);
        return Result.success(pageBean);
    }

    // 根据id修改员工信息
    @PutMapping("/update")
    public Result updateMember(@RequestBody Member member) {
        log.info("根据id修改员工信息，id:{}", member.getId());
        memberService.update(member);
        return Result.success();
    }

    // 根据id删除工坊成员
    @DeleteMapping("/delete")
    public Result deleteMember(@RequestParam int id) {
        log.info("根据id删除工坊成员：{}",id);
        memberService.delete(id);
        return Result.success();
    }

    //添加工坊成员
    @PostMapping("/add")
    public Result addMember(@RequestBody Member member) {
        log.info("添加工坊成员：{}",member);
        memberService.add(member);
        return Result.success();
    }

}
